﻿using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;

namespace Song.Common.Tools
{
    /// <summary>
    /// String类型的扩展方法
    /// </summary>
    public static class StringExtensions
    {
        /// <summary>
        /// Email 的验证格式正则表达式
        /// </summary>
        public const string MailPattern = @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
        /// <summary>
        /// DateTime的验证格式正则表达式
        /// </summary>
        public const string DateTimePattern = @"/^(\d{4})-(0\d{1}|1[0-2])-(0\d{1}|[12]\d{1}|3[01])$/";
        /// <summary>
        /// Windows路径的验证格式正则表达式
        /// </summary>
        public const string WindowsPathPattern = @"\b[a-zA-Z]:\\(\w+\\?)+\z";

        /// <summary>
        /// 判断是否为空字符串
        /// </summary>
        /// <param name="s">字符串</param>
        /// <returns>判断结果</returns>
        public static bool IsNullOrEmpty(this string s)
        {
            return string.IsNullOrEmpty(s);
        }

        /// <summary>
        /// 判断是否符合正则表达式
        /// </summary>
        /// <param name="s"></param>
        /// <param name="pattern"></param>
        /// <returns></returns>
        public static bool IsMatch(this string s, string pattern)
        {
            return !s.IsNullEmpty() && Regex.IsMatch(s, pattern);
        }
        /// <summary>
        /// 获取匹配结果
        /// </summary>
        /// <param name="s">要匹配的字符串</param>
        /// <param name="pattern">规则</param>
        /// <returns>匹配结果</returns>
        public static string Match(this string s, string pattern)
        {
            return s == null ? "" : Regex.Match(s, pattern).Value;
        }
        /// <summary>
        /// 验证字符串是否是Email格式
        /// </summary>
        /// <param name="str">要验证字符串</param>
        /// <returns>True：符合格式；False：不符合格式</returns>
        public static bool IsEmail(this string str)
        {
            return str.IsMatch(MailPattern);
        }
        /// <summary>
        /// 判断是否是合法的日期格式
        /// </summary>
        /// <param name="str">要判断的字符串</param>
        /// <returns>判断结果</returns>
        public static bool IsDateTime(this string str)
        {
            return str.IsMatch(DateTimePattern);
        }

        public static bool IsInt(this string s)
        {
            int i;
            return int.TryParse(s, out i);
        }

        public static int ToInt(this string s)
        {
            return int.Parse(s);
        }

        #region 强大的扩展，慎用

        public static void Open(this string s)
        {
            Process.Start(s);
        }
        public static void CreateDirectory(this string path)
        {
            Directory.CreateDirectory(path);
        }
        public static void WriteText(this string path, string contents)
        {
            File.WriteAllText(path, contents);
        }
        public static void DeleteFile(this string path)
        {
            if (File.Exists(path)) File.Delete(path);
        }
        public static string ExecuteDOS(this string cmd, out string error)
        {
            Process process = new Process();
            process.StartInfo.FileName = "cmd.exe";
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardInput = true;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardError = true;
            process.StartInfo.CreateNoWindow = true;
            process.Start();
            process.StandardInput.WriteLine(cmd);
            process.StandardInput.WriteLine("exit");
            error = process.StandardError.ReadToEnd();
            return process.StandardOutput.ReadToEnd();
        }

        public static string ExecuteDOS(this string cmd)
        {
            var process = new Process
                                  {
                                      StartInfo =
                                          {
                                              FileName = "cmd.exe",
                                              UseShellExecute = false,
                                              RedirectStandardInput = true,
                                              RedirectStandardOutput = true,
                                              RedirectStandardError = true,
                                              CreateNoWindow = true
                                          }
                                  };
            process.Start();
            process.StandardInput.WriteLine(cmd + "& exit");
            process.StandardInput.WriteLine("exit");
            return process.StandardOutput.ReadToEnd();
        }
        #endregion 强大的扩展，慎用

    }
}
